<?php

/**
 * This is the model class for table "edu_comment".
 *
 * The followings are the available columns in table 'edu_comment':
 * @property integer $id
 * @property string $username
 * @property string $content
 * @property integer $parentid
 * @property integer $createtime
 * @property integer $supports
 * @property integer $opposition
 * @property integer $userid
 * @property string $ip
 * @property integer $close
 */
class Comment extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Comment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'edu_comment';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('parentid, createtime, supports, opposition, userid, close', 'numerical', 'integerOnly'=>true),
			array('username', 'length', 'max'=>30),
			array('ip', 'length', 'max'=>15),
			array('content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, content, parentid, createtime, supports, opposition, userid, ip, close', 'safe', 'on'=>'search'),
			array('parentid,content','safe','on'=>'reply'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Username',
			'content' => 'Content',
			'parentid' => 'Parentid',
			'createtime' => 'Createtime',
			'supports' => 'Supports',
			'opposition' => 'Opposition',
			'userid' => 'Userid',
			'ip' => 'Ip',
			'close' => 'Close',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('parentid',$this->parentid);
		$criteria->compare('createtime',$this->createtime);
		$criteria->compare('supports',$this->supports);
		$criteria->compare('opposition',$this->opposition);
		$criteria->compare('userid',$this->userid);
		$criteria->compare('ip',$this->ip,true);
		$criteria->compare('close',$this->close);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}


	public function beforeSave()
	{
		if($this->isNewRecord)
		{
			$this->createtime = time();
		}
		return parent::beforeSave();
	}

	public function afterSave()
	{
		$sql ="update edu_article set comments=comments+1 where id=:id";
		$id =$this->parentid;
		Yii::app()->db->createCommand($sql)->bindParam(":id",$id)->execute();
		return parent::beforeSave();
	}

}